/**********************************************************
 * AdminLoginInterceptor.java
 * Created by ankur
 * Last modified Date: Aug 26, 2008 8:30:56 AM by ankur
 * Version : 0.1
 * Copyright (c) 2008 - 2008 cyberThink Infotech Pvt. Ltd., All rights reserved.
 **********************************************************/

package com.printeverywhere.interceptor;

import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.StrutsStatics;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.interceptor.I18nInterceptor;
import com.printeverywhere.action.CreateWantedAction;
import com.printeverywhere.delegator.TranslationSystemServiceDelegator;
import com.printeverywhere.dto.Languages;
import com.printeverywhere.util.AdminConstants;
import com.printeverywhere.util.CommonUtil;

/**
 * The Class AdminLoginInterceptor.
 */
public class LanguageInterceptor extends AbstractInterceptor implements StrutsStatics {

	private static final String ALIGNRIGHT = "alignright";
	private static final String RIGHT = "right";
	private static final String LEFT = "left";
	private static final String ALIGNLEFT = "alignleft";
	private static final String LTR = "ltr";
	private static final String REQUEST_LOCALE = "request_locale";

	private String configurationUrl;

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;
	private String locale;
	/** The logger. */
	private static Logger logger = Logger.getLogger(LanguageInterceptor.class);

	/* (non-Javadoc)
	 * @see com.opensymphony.xwork2.interceptor.AbstractInterceptor#intercept(com.opensymphony.xwork2.ActionInvocation)
	 */
	public String intercept(ActionInvocation invocation) throws Exception {
		Languages defaultLanguage =CommonUtil.getDefaultLanguages((List<Languages>)invocation.getInvocationContext().getContext().getApplication().get(AdminConstants.LANGUAGES));
		Locale defaultLocale = new Locale(defaultLanguage.getLanguageCode());
		logger.info("Starting synchronized Language interceptor - DL :: ".intern() + defaultLanguage.getLanguageCode());

		synchronized (this) {
			// Get the action context from the invocation so we can access the
			// HttpServletRequest and HttpSession objects.
			final ActionContext context = invocation.getInvocationContext();
			HttpServletRequest request = (HttpServletRequest) context.get(HTTP_REQUEST);
			HttpSession session = request.getSession();
			String language = null;

			String tmpLanguage = null;
			if(request.getParameter(REQUEST_LOCALE.intern()) != null){
				tmpLanguage=request.getParameter(REQUEST_LOCALE.intern());
			}else{
				if(session.getAttribute("WW_TRANS_I18N_LOCALE") != null){
					tmpLanguage=session.getAttribute("WW_TRANS_I18N_LOCALE").toString();
				}
			}

			if(tmpLanguage != null) {
				List<Languages> languageList = (List<Languages>) context.getApplication().get(AdminConstants.LANGUAGES);
				for (Languages languages : languageList) {
					if(languages.getLanguageCode().equalsIgnoreCase(tmpLanguage)) {
						language = tmpLanguage;
						if(languages.getThemeId().getId() == 1){
							configurationUrl = "/jsp/front";
						}else{
							configurationUrl = "/jsp/front/"+languages.getThemeId().getFloderName();
						}
						CommonUtil.setValue(Class.forName(invocation.getAction().getClass().getName()), invocation.getAction(), "configurationUrl", configurationUrl);
						break;
					}
				}
			}

			if(language!=null && language.length()>0){
				Languages languages=TranslationSystemServiceDelegator.get(language);
				if(languages!=null){
					if(languages.getThemeId().getId() == 1){
						configurationUrl = "/jsp/front";
					}else{
						configurationUrl = "/jsp/front/"+languages.getThemeId().getFloderName();
					}
					CommonUtil.setValue(Class.forName(invocation.getAction().getClass().getName()), invocation.getAction(), "configurationUrl", configurationUrl);
					session.setAttribute(AdminConstants.LANGUAGEFORCSS,languages);
					session.setAttribute(AdminConstants.TEXT,languages.getText());
					locale = languages.getText();
					session.setAttribute(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE,new Locale(language));
					invocation.getInvocationContext().setLocale(new Locale(language));
					if(locale.equalsIgnoreCase(LTR)){
						session.setAttribute(ALIGNLEFT, LEFT);
						session.setAttribute(ALIGNRIGHT, RIGHT);
					}else{
						session.setAttribute(ALIGNLEFT, RIGHT);
						session.setAttribute(ALIGNRIGHT, LEFT);
					}
				}else{
					if(defaultLanguage.getThemeId().getId() == 1){
						configurationUrl = "/jsp/front";
					}else{
						configurationUrl = "/jsp/front/"+defaultLanguage.getThemeId().getFloderName();
					}
					CommonUtil.setValue(Class.forName(invocation.getAction().getClass().getName()), invocation.getAction(), "configurationUrl", configurationUrl);

					locale  = LTR;
					session.setAttribute(AdminConstants.TEXT,LTR);
					session.setAttribute(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE,defaultLocale);
					invocation.getInvocationContext().setLocale(defaultLocale);
					session.setAttribute(AdminConstants.LANGUAGEFORCSS,defaultLanguage);
					session.setAttribute(ALIGNLEFT, LEFT);
					session.setAttribute(ALIGNRIGHT, RIGHT);
				}
			}else{
				if(defaultLanguage.getThemeId().getId() == 1){
					configurationUrl = "/jsp/front";
				}else{
					configurationUrl = "/jsp/front/"+defaultLanguage.getThemeId().getFloderName();
				}
				CommonUtil.setValue(Class.forName(invocation.getAction().getClass().getName()), invocation.getAction(), "configurationUrl", configurationUrl);

				logger.debug("Text value :: " + session.getAttribute(AdminConstants.TEXT));
				if(session.getAttribute(AdminConstants.TEXT)!=null){
					locale = (String)session.getAttribute(AdminConstants.TEXT);
					if(locale.equalsIgnoreCase(LTR)){
						session.setAttribute(ALIGNLEFT, LEFT);
						session.setAttribute(ALIGNRIGHT, RIGHT);
					}else{
						session.setAttribute(ALIGNLEFT, RIGHT);
						session.setAttribute(ALIGNRIGHT, LEFT);
					}
					return invocation.invoke();
				}
				Locale test= (Locale)session.getAttribute(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE);
				if(test==null){
					test=defaultLocale;
					session.setAttribute(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE,test);
					invocation.getInvocationContext().setLocale(test);
					session.setAttribute(AdminConstants.LANGUAGEFORCSS,defaultLanguage);
					session.setAttribute(ALIGNLEFT, LEFT);
					session.setAttribute(ALIGNRIGHT, RIGHT);
				}
				Languages languages=TranslationSystemServiceDelegator.get(test.getLanguage());
				if(languages!=null){
					session.setAttribute(AdminConstants.TEXT,languages.getText());
					session.setAttribute(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE,defaultLocale);
					invocation.getInvocationContext().setLocale(defaultLocale);
					session.setAttribute(AdminConstants.LANGUAGEFORCSS,languages);
					if(languages.getText().equalsIgnoreCase(LTR)){
						session.setAttribute(ALIGNLEFT, LEFT);
						session.setAttribute(ALIGNRIGHT, RIGHT);
					}else{
						session.setAttribute(ALIGNLEFT, RIGHT);
						session.setAttribute(ALIGNRIGHT, LEFT);
					}
				}
				else {
					if(defaultLanguage.getThemeId().getId() == 1){
						configurationUrl = "/jsp/front";
					}else{
						configurationUrl = "/jsp/front/"+defaultLanguage.getThemeId().getFloderName();
					}
					CommonUtil.setValue(Class.forName(invocation.getAction().getClass().getName()), invocation.getAction(), "configurationUrl", configurationUrl);

					session.setAttribute(AdminConstants.TEXT,LTR);
					session.setAttribute(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE,defaultLocale);
					invocation.getInvocationContext().setLocale(defaultLocale);
					session.setAttribute(AdminConstants.LANGUAGEFORCSS,defaultLanguage);
					session.setAttribute(ALIGNLEFT, LEFT);
					session.setAttribute(ALIGNRIGHT, RIGHT);
				}
			}

			return invocation.invoke();
		}
	}

	/**
	 * @return the locale
	 */
	public String getLocale() {
		return locale;
	}

	/**
	 * @param locale the locale to set
	 */
	public void setLocale(String locale) {
		this.locale = locale;
	}

	public String getConfigurationUrl() {
		return configurationUrl;
	}

	public void setConfigurationUrl(String configurationUrl) {
		this.configurationUrl = configurationUrl;
	}
}